/**
 * Download.java
 * @author: liuex
 * 2007-12-30 ����07:17:23
 *
 * note: 
 */
package lex.mtd;

import javax.swing.ActionMap;
import javax.swing.JFrame;

import org.lex.app.Application;
import org.lex.swing.Swings;

import lex.mtd.core.test.DownloadTest;
import lex.mtd.core.utils.LogSet;
import lex.mtd.ui.MainContentPane;
import lex.mtd.ui.MainMenuBar;
import lex.mtd.ui.actions.NewTaskAction;
import lex.mtd.ui.actions.RemoveTaskAction;
import lex.mtd.ui.actions.StartAction;
import lex.mtd.ui.actions.StopAction;
import lex.mtd.utils.Actions;
import lex.mtd.utils.prefs.Keys;

public class Download extends Application
{
	private static final Boolean DEBUG = true;

	public static void main( String[] args )
	{
		Swings.setUseBoldMetalFont( false );
		Application.launch( Download.class, args );
	}

	public static Download getApplication()
	{
		return Application.getApplication( Download.class );
	}

	private JFrame mainFrame = null;
	private MainContentPane contentPane = null;

	@Override
	public String getApplicationID()
	{
		return "mtd";
	}

	public JFrame getRootWindow()
	{
		if( null == mainFrame )
		{
			mainFrame = new JFrame();
			mainFrame.setName( getClass().getName() + ".mainframe" );
			mainFrame.setTitle( "Download Demo" );
			mainFrame.setContentPane( this.getContentPane() );
			mainFrame.setJMenuBar( new MainMenuBar() );
		}
		return mainFrame;
	}

	/*****************
	 * shortcut
	 **************/
	public LogSet getLogSet()
	{
		return this.getContentPane().getDetailsPane().getLogPane();
	}

	public MainContentPane getContentPane()
	{
		if( null == contentPane )
			contentPane = new MainContentPane();
		return contentPane;
	}

	@Override
	protected void initialize( String[] args )
	{
		Keys.loadDefaultConfiguration( getContext().getResourceMap() );
		ActionMap am = this.getContext().getActionMap();
		am.put( Actions.NEW_TASK, new NewTaskAction() );
		am.put( Actions.REMOVE_TASK, new RemoveTaskAction() );
		am.put( Actions.START_SELECTED_TASKS, new StartAction() );
		am.put( Actions.STOP_SELECTED_TASKS, new StopAction() );
		// =====================================================
		this.getContext().getSessionStorage().restore( this.getRootWindow() );
		if( DEBUG )
		{
			this.getContentPane().addDownloadTask( DownloadTest.getFTPTask() );
			this.getContentPane().addDownloadTask( DownloadTest.getHTTPTask() );
		}
		this.mainFrame.setVisible( true );
	}

	@Override
	protected void shutdown()
	{
		this.getContext().getSessionStorage().save( this.getRootWindow() );
	}
}
